<?php
/**
 * Static content controller.
 *
 * This file will render views from views/pages/
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.Controller
 * @since         CakePHP(tm) v 0.2.9
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 */

App::uses('AppController', 'Controller');
App::uses('Blogs', 'Model');

/**
 * Static content controller
 *
 * Override this controller by placing a copy in controllers directory of an application
 *
 * @package       app.Controller
 * @link http://book.cakephp.org/2.0/en/controllers/pages-controller.html
 */
class BlogsController extends AppController {
    public $helpers = array('Html','Form');
    
    public function init() {
        
    }
/**
 * This controller does not use a model
 *
 * @var array
 */
	public $uses = array();

/**
 * Displays a view
 *
 * @param mixed What page to display
 * @return void
 * @throws NotFoundException When the view file could not be found
 *	or MissingViewException in debug mode.
 */
	public $paginate = array(
            //'conditions' => array('Blogs.title LIKE' => 'a%'),
	        'order' => 'created_date DESC',
            'limit' => 2
        );
	public function index($params='') {
	    $this->loadModel('Blogs');
	    /* $this->Paginator->settings = array(
            'conditions' => array('Recipe.title LIKE' => 'a%'),
	        'order' => 'Blogs.created_date DESC',
            'limit' => 10
        ); */
	    
	    // similar to findAll(), but fetches paged results
	    
	    $this->Paginator;
	    $keyword = $this->request->query('keyword');
	    $_GET['keyword'] = $keyword;
	    if(isset($keyword) && $keyword!=''){
	    	$this->paginate=array(
	            'conditions' => array('title LIKE' => '%'.$keyword.'%'),
		        'order' => 'created_date DESC',
	            'limit' => 2
	        );
	    }
	    $params=$this->request->params;
	    if(isset($params['named']['keyword']) && $params['named']['keyword']!=''){
	    	$this->paginate=array(
	    			'conditions' => array('title LIKE' => '%'.$params['named']['keyword'].'%'),
	    			'order' => 'created_date DESC',
	    			'limit' => 2
	    	);
	    }
	    $this->set('blogs',$this->paginate());
	    
		$this->render('index');
	}
	public function edit($id=null) {
	    if(!$id){
	        throw new NotFoundException('Not found page!!!');
	    }
	    $blog = $this->Blogs->findById($id);
	    if(!$blog){
	        throw new NotFoundException('Not found page!!!');
	    }
	    $this->set('blog',$blog);
	    $this->render('save');
	}
	public function save($id=null) {
	    $this->loadModel('Blogs');
	    if(!$id || $id==null){
	        if ($this->request->is('post')) {
	            $this->Blogs->create();
	            $this->Blogs->set($this->request->data);
	            if ($this->Blogs->validates()) {
		            $this->Blogs->saveField('created_date',date('Y-m-d H:i:s',time()));
		            if ($this->Blogs->save($this->request->data)) {
		                // Set a session flash message and redirect.
		                //$this->Session->setFlash('Create blog is successful!');
		                return $this->redirect('/blogs/index');
		            }
	            } else {
	            	$errors = $this->ModelName->validationErrors;
	            }
	            
	        }
	        $this->set('blog', null);
	    }else{
	        $blog = $this->Blogs->findById($id);
	        if($blog){
	            
	            if ($this->request->is('post')) {
	                $this->Blogs->id=$id;
	                $this->Blogs->saveField('modified_date',date('Y-m-d H:i:s',time()));
	                if ($this->Blogs->save($this->request->data)) {
	                    // Set a session flash message and redirect.
	                    //$this->Session->setFlash('Update blog is successful!');
	                    return $this->redirect('/blogs/index');
	                }
	            }
	            $this->set('blog', $blog);
	        }else{
	            throw new NotFoundException('Not found page!!!');
	        }
	    }
	    
	    $this->render('save');
	}
	public function delete($id=null) {
	    $this->loadModel('Blogs');
	    if(!is_numeric($id) || $id==null){
	        throw new NotFoundException('Not found page!!!');
	    }
	    //$blog = $this->Blogs->findById($id);
	    $this->Blogs->id=$id;
	    if($this->Blogs->delete($id)){
	        $this->Blogs->delete($blog['Blogs']['id']);
	    }else{
	        throw new NotFoundException('Not found page!!!');
	    }
	    $this->redirect(array('controller'=>'blogs','action'=>'index'));
	}
}
